Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
How store address of an array[] in a variable

This seems like a silly question. I have an array of chars and want to store the address of the array in another variable, but can't seem to declare the correct type for the array address (I'm using gcc):


IN:

int main(void){
  char cha[] = "abcde";
  char **arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:25: warning: initialization of ‘char **’ from incompatible pointer type ‘char (*)[6]’ [-Wincompatible-pointer-types]
    3 |   char **arrayAddress = &cha;
      |                         ^

This is expected, I have read elsewhere that the type of cha should be char(*)[6]. But when I try to declare arrayAddress with this type, my program fails:


IN:

int main(void){
  char cha[] = "abcde";
  char (*)[6]arrayAddress = &cha;
}

OUT:

arrayaddress.c: In function ‘main’:
arrayaddress.c:3:10: error: expected identifier or ‘(’ before ‘)’ token
    3 |   char (*)[6]arrayAddress = &cha;
      |          ^
make: *** [<builtin>: arrayaddress] Error 1
       ^

How do I define arrayAddress correctly?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

It is written:

char (*arrayAddress)[6] = &cha;

Notice that the name of variable gets tucked in middle of the expression.

over 4 years ago · Santiago Trujillo Denunciar

0

Arrays decay to pointers.

  char cha[] = "abcde";

  char *p1 = cha;

  char (*arrayptr)[sizeof(cha)] = &cha;

cha, &cha[0] and &cha reference the same first element of the array cha, the only difference is the type.

  1. cha and &cha[0] has type: pointer to char
  2. &cha has type: pointer to array of 6 char elements.
over 4 years ago · Santiago Trujillo Denunciar

0

If your compiler supports typeof extension (gcc does) then you can define the pointer as:

typeof(char (*)[6]) arrayAddress = &cha;

Or even cleaner as:

typeof(char[6]) * arrayAddress = &cha;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda